# questionnaire/models.py
from django.db import models
from django.conf import settings

class QuestionQ1(models.Model):
    question = models.TextField()
    option_a = models.CharField(max_length=255)
    option_b = models.CharField(max_length=255)
    option_c = models.CharField(max_length=255)
    option_d = models.CharField(max_length=255)
    answer = models.CharField(max_length=1)

    class Meta:
        db_table = 'question_q1'

# 同样为 QuestionQ2 - QuestionQ5 添加 correct_answer 字段
class QuestionQ2(models.Model):
    question = models.TextField()
    option_a = models.CharField(max_length=255)
    option_b = models.CharField(max_length=255)
    option_c = models.CharField(max_length=255)
    option_d = models.CharField(max_length=255)
    answer = models.CharField(max_length=1)

    class Meta:
        db_table = 'question_q2'

class QuestionQ3(models.Model):
    question = models.TextField()
    option_a = models.CharField(max_length=255)
    option_b = models.CharField(max_length=255)
    option_c = models.CharField(max_length=255)
    option_d = models.CharField(max_length=255)
    answer = models.CharField(max_length=1)

    class Meta:
        db_table = 'question_q3'

class QuestionQ4(models.Model):
    question = models.TextField()
    option_a = models.CharField(max_length=255)
    option_b = models.CharField(max_length=255)
    option_c = models.CharField(max_length=255)
    option_d = models.CharField(max_length=255)
    answer = models.CharField(max_length=1)

    class Meta:
        db_table = 'question_q4'

class QuestionQ5(models.Model):
    question = models.TextField()
    option_a = models.CharField(max_length=255)
    option_b = models.CharField(max_length=255)
    option_c = models.CharField(max_length=255)
    option_d = models.CharField(max_length=255)
    answer = models.CharField(max_length=1)

    class Meta:
        db_table = 'question_q5'

class UserAnswer(models.Model):
    # 修改这一行，使用 settings.AUTH_USER_MODEL 而不是 'auth.User'
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    question_type = models.CharField(max_length=2)
    correct_count = models.IntegerField()
    total_count = models.IntegerField()

    class Meta:
        db_table = 'user_answer'

# 在现有模型下方添加
class UserTestResult(models.Model):
    """用户问卷测试结果模型"""
    user = models.ForeignKey('users.User', on_delete=models.CASCADE, related_name='test_results')
    question_type = models.CharField(max_length=10, verbose_name='题型')
    correct_count = models.IntegerField(default=0, verbose_name='正确数量')
    total_count = models.IntegerField(default=0, verbose_name='总题数')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'tb_user_test_result'
        verbose_name = '用户测试结果'
        verbose_name_plural = verbose_name
        unique_together = ('user', 'question_type')  # 每个用户每种题型只有一条记录